<?php
// +----------------------------------------------------------------------
// | JIANKE [ WWW.XYSER.COM ]
// +----------------------------------------------------------------------
// | Copyright (c) 2015 http://xyser.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( dingdayu @ JIANKE )
// +----------------------------------------------------------------------
// | Author: dingdayu <614422099@qq.com>
// +----------------------------------------------------------------------
// | DATE: 2015-11-4 18:59
// +----------------------------------------------------------------------

namespace Home\Controller;
use Think\Controller;

class BijiaController extends Controller {
    private $CURL_URL = "http://www.beianbeian.com/s?keytype=1&q=";

    public function index(){
        $view_count = cookie("view_count");
        if(!empty($view_count)){
            cookie("view_count",$view_count + 1);
        }else{
            cookie("view_count", 1);
        }
        $data = self::getIndex2(I('p'));
        $this->assign('data',$data);
        $this->title = "白菜价";
        $this->url = U('Bijia/index');
        $this->display();
    }

    public function c99(){
        $view_count = cookie("view_count");
        if(!empty($view_count)){
            cookie("view_count",$view_count + 1);
        }else{
            cookie("view_count", 1);
        }
        $data = self::get99();
        $this->assign('data',$data);
        $this->title = "9.9元包邮";
        $this->url = U('Bijia/c99');
        $this->display('index');
    }

    public function getIndex(){
        $url = "http://cu.manmanbuy.com/DefaultSharev2.aspx?DA=Mon%20Nov%2016%202015%2016:17:50%20GMT+0800%20(%D6%D0%B9%FA%B1%EA%D7%BC%CA%B1%BC%E4)&action=Pull-downLoad&keywordsall=&keywords=&tags=&siteid=0";

        $html_content = S(md5($url.cookie('PHPSESSID')));
        if(empty($html_content)){
            $html_content = self::curl($url);
            S(md5($url.cookie('PHPSESSID')),$html_content);
        }
        echo $html_content;
        $html_content = gbk2utf8($html_content);

        preg_match_all('%<li class="item">(.*?)<\/li>%si',$html_content,$lis);
        //dump($lis);

        $data =array();
        foreach($lis[0] as $k => $v){
            //dump($v);
            preg_match('%title="(.*?)">%si',$v,$title);
            //dump($title);
            $data[$k]['title'] = trim($title[1]);
            preg_match('%src="(.*?)">%si',$v,$src);
            $data[$k]['src'] = trim($src[1]);

            preg_match('%<div class="price">(.*?)<\/div>%si',$v,$price);
            $data[$k]['price'] = trim($price[1]);

            preg_match('%<div class="mall">(.*?)<\/div>%si',$v,$mall);
            $data[$k]['mall'] = trim($mall[1]);


            preg_match('%class="golink"><a href="(.*?)"  target%si',$v,$href);
            $href2 = explode('tourl=',$href[1]);
            //dump($href2);
            $data[$k]['href'] = urldecode(trim($href2[1]));
        }
        //dump($data);
        return $data;
    }

    public function getIndex2($p = 1){
        $url = "http://baicai.manmanbuy.com/Default.aspx?PageID=".$p;

        $html_content = S(md5($url.cookie('PHPSESSID')));
        if(empty($html_content)){
            $html_content = self::curl($url);
            S(md5($url.cookie('PHPSESSID')),$html_content);
        }
        //echo $html_content;
        $html_content = gbk2utf8($html_content);

        preg_match('%<div id="ctl00_ContentPlaceHolder1_listhdinfo" style="width:1220px;">(.*?)<div id="ctl00_ContentPlaceHolder1_divpage" style=" margin:20px;">%si',$html_content,$lis);
        //dump($lis);
        preg_match_all('%<div class="info2 masonry-brick">(.*?)<div class="clear"></div>%si',$lis[0],$lis2);
        //dump($lis2);

        $data =array();
        foreach($lis2[0] as $k => $v){
            //dump($v);
            preg_match('%<img alt="(.*?)" src=%si',$v,$title);
            //dump($title);
            $data[$k]['title'] = trim($title[1]);

            preg_match('%src="(.*?)"%si',$v,$src);
            $data[$k]['src'] = trim($src[1]);

            preg_match('%<span class=\'rmbfh\'>￥</span>(.*?)</span>%si',$v,$price);
            $data[$k]['price'] = trim($price[1]);
            if(strpos($v, '<i class="icon-baoyou"></i>') !== false){
                $data[$k]['price'] =  "[包邮]".$data[$k]['price']."元";
            }else{
                $data[$k]['price'] =  $data[$k]['price']."元";
            }

            preg_match('%<div class="name">(.*?)<\/div>%si',$v,$mall);
            $data[$k]['mall'] = trim($mall[1]);


            preg_match('%isconvert=1 href="(.*?)"%si',$v,$href);
            $data[$k]['href'] = urldecode(trim($href[1]));
        }
        //dump($data);
        //exit();
        return $data;
    }
    public function get99(){
        $url = "http://baicai.manmanbuy.com/Default_New.aspx?DA=Mon%20Nov%2016%202015%2015:16:02%20GMT+0800%20(%D6%D0%B9%FA%B1%EA%D7%BC%CA%B1%BC%E4)&action=Pull-downLoad&zcid=0&ipage=3&protype=jiukai&key=&classpy=0&orderby=id";

        //$html_content = S(md5($url.cookie('PHPSESSID')));
        if(empty($html_content)){
            $html_content = self::curl($url);
            S(md5($url.cookie('PHPSESSID')),$html_content);
        }
        //echo $html_content;
        $html_content = gbk2utf8($html_content);

        preg_match_all('%<li>(.*?)<\/li>%si',$html_content,$lis);
        //dump($lis);

        $data =array();
        foreach($lis[0] as $k => $v){

            //dump($v);
            preg_match('%title="(.*?)">%si',$v,$title);
            //dump($title);
            $data[$k]['title'] = trim($title[1]);
            preg_match('%src="(.*?)">%si',$v,$src);
            $data[$k]['src'] = trim($src[1]);

            preg_match('%<span class="price-current"><em>&yen;<\/em>(.*?)<\/span>%si',$v,$price);
            $data[$k]['price'] = trim($price[1]);

            if(strpos($v, '<i class="icon-baoyou"></i>') !== false){
                $data[$k]['price'] =  "[包邮]".$data[$k]['price']."元";
            }else{
                $data[$k]['price'] =  $data[$k]['price']."元";
            }

            preg_match('%<div class="name">(.*?)<\/div>%si',$v,$mall);
            $mall2 = explode('</i>',$mall[1]);
            //dump($mall2);
            $data[$k]['mall'] = trim($mall2[1]);


            preg_match('%isconvert=1 href="(.*?)" target="_blank">%si',$v,$href);
            $data[$k]['href'] = urldecode(trim($href[1]));
        }
        //dump($data);
        return $data;
    }

    public function search($key = ""){
        $url = "http://m.manmanbuy.com/Search.aspx?key=";
    }

    //一下是支持功能
    private function curl($uil){
        // 初始化一个 cURL 对象
        $curl = curl_init();
        // 设置你需要抓取的URL
        curl_setopt($curl, CURLOPT_URL, $uil);

        curl_setopt($curl, CURLOPT_REFERER, 'http://www.baidu.com');//伪装一个来路
        curl_setopt($curl, CURLOPT_USERAGENT, 'Baiduspider+(+http://www.baidu.com/search/spider.htm)'); //伪装成百度蜘蛛

        $cookie_jar = dirname(__FILE__)."\\cookiess\\".cookie('PHPSESSID').".cookie";
        curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie_jar); //保存
        curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie_jar); //读取

        curl_setopt($curl, CURLOPT_FOLLOWLOCATION,1); //抓取转跳
        // 设置header
        curl_setopt($curl, CURLOPT_HEADER, 0);
        // 设置cURL 参数，要求结果保存到字符串中还是输出到屏幕上。
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        // 运行cURL，请求网页
        $data = curl_exec($curl);
        // 关闭URL请求
        curl_close($curl);

        return $data;
    }

    public function caiji($beianhao = ''){
        $html_content = self::curl($this->CURL_URL .$beianhao);

        //提取table
        preg_match_all('%<table id="show_table"(.*?)<\/table>%si',$html_content,$table);
        //print_r($table);
        $error = preg_match('/没有符合条件的记录, 即未备案/',$table[0][0]);
        if($error) return 0;

        //提取行
        preg_match_all('%<tr>(.*?)<\/tr>%si',$table[0][0],$tr);
        //print_r($tr);
        //echo count($tr[0]);

        //循环提取每行
        for($i=1;$i < count($tr[0])-2  ;$i++){
            preg_match_all('%<td(.*?)<\/td>%si',$tr[0][$i],$td);
            //print_r($td);

            //匹配具体的值项
            preg_match('%<div id="kind">(.*?)<\/div>%si',$td[0][1],$owner);
            preg_match('%<div id="kind">(.*?)<\/div>%si',$td[0][2],$type);
            preg_match('%target="_blank">(.*?)<\/a>%si',$td[0][3],$beianhao_data);
            preg_match('%>(.*?)<%si',$td[0][4],$website_name);
            preg_match('%target="_blank">(.*?)<\/a>%si',$td[0][5],$website_url);
            preg_match('%<div id="pass_time">(.*?)<\/div>%si',$td[0][6],$audit_time);
            preg_match('%href="(.*?)"%si',$td[0][7],$detailed);

            $data[$i-1]['owner'] = trim($owner[1]);
            $data[$i-1]['type'] = trim($type[1]);
            $data[$i-1]['beianhao_2'] = trim(strip_tags($beianhao_data[1]));
            $beianhao_data_arr = explode("-",$data[$i-1]['beianhao_2']);
            $data[$i-1]['beianhao'] = $beianhao_data_arr[0];
            $data[$i-1]['website_name'] = trim($website_name[1]);
            $data[$i-1]['audit_time'] = trim($audit_time[1]);
            $data[$i-1]['website_url'] = trim($website_url[1]);
            $data[$i-1]['detailed'] = trim($detailed[1]);
            $data[$i-1]['time'] = time();
            $data[$i-1]['short'] = msubstr($beianhao_data_arr[0],0,1,'utf-8',false);
        }

        //dump($data);
        return $data;
    }

    private function get_log_count($short){
        $ret_short = M('BeianLog')->where(array('short'=>$short))->order('short_count desc')->getField('short_count');
        if($ret_short !== false){
            return $ret_short;
        }
        return 0;
    }


    private function save_log($short,$short_count,$data){
        if($data == 0){
            M("BeianLog")->add(array('short'=>$short,'short_count'=>$short_count,'countent'=>$data,'add_time'=>time()));
            return true;
        }
        if(is_array($data)){
            M("BeianLog")->add(array('short'=>$short,'short_count'=>$short_count,'countent'=>serialize($data),'add_time'=>time()));
            return true;
        }
    }

    private function save_data($data){
        if(is_array($data)){
            if(array_depth($data) > 1){
                M("Beian")->addAll($data);
                return true;
            }else{
                M("Beian")->add($data);
                return true;
            }
        }
    }
}